/**
 * Created by MrLiu on 2016/3/23
 * 表格排序插件
 */
(function ($) {
    "use strict";
    var old = null;
    var TableSorting = function (element, options) {
        this.init(element, options);
    };

    TableSorting.prototype = {
        init: function (element, options) {
            this.$element = $(element);
            this.setOptions(options);
            this.initialized = true;
        },
        setOptions: function (options) {
            this.options = $.extend({}, (this.options || $.fn.tableSorting.defaults), options);
            this.listen();
            this.render();
        },
        listen: function () {
            this.$element.off("sorting-changed-callback");// unload the events for the element
            if (typeof (this.options.onCallback) === "function") {
                this.$element.on("sorting-changed-callback", this.options.onCallback);
            }
        },
        destroy: function () {
            this.$element.off("sorting-changed-callback");
            this.$element.removeData('tableSorting');
        },
        getSortingInfo:function(){
            var _data = this.$element.data("tableSorting");
            var output = {
                name : _data.options.sortingName,
                type : _data.options.sorting
            }
            return output;
        },
        setDefaultSortColumn : function () {
            var _this = this;
            var $ele = this.$element;
            var thisOptions = this.options;
            var defaultSortingName = thisOptions.name;
            var defaultSortingType = thisOptions.type;
            if(defaultSortingName){
                var defaultSortingColumn = $ele.find(".sorting[data-sorting-name='"+defaultSortingName+"']");
                if(defaultSortingColumn.length>0){
                    //重置
                    if(defaultSortingType == "asc"){
                        //升序
                        defaultSortingColumn.removeClass("sorting_asc sorting_desc");
                        defaultSortingColumn.addClass("sorting_asc");
                    }else if(defaultSortingType == "desc"){
                        //降序
                        defaultSortingColumn.removeClass("sorting_asc sorting_desc");
                        defaultSortingColumn.addClass("sorting_desc");
                    }
                    defaultSortingColumn.siblings(".sorting").removeClass("sorting_asc sorting_desc");
                }
            }
        },
        render: function () {
            var _this = this;
            var $ele = this.$element;
            var thisOptions = this.options;
            //设置默认的排序列
            this.setDefaultSortColumn();

            $ele.find(".sorting").click(function(){
                var name = $(this).attr("data-sorting-name");
                var type;
                //如果当前是升序,则降序,否则,则升序
                if($(this).hasClass("sorting_asc")){
                    //降序
                    type = "desc";
                    $(this).removeClass("sorting_asc").addClass("sorting_desc");
                }else{
                    //升序
                    type = "asc";
                    $(this).removeClass("sorting_desc").addClass("sorting_asc");
                }
                //将所有兄弟元素的排序重置为默认
                $(this).siblings(".sorting").removeClass("sorting_asc sorting_desc");

                //设置data值,如果外部需要,则可以直接获取
                var _data = $ele.data("tableSorting");
                _data.options.sorting = type;
                _data.options.sortingName = name;
                $ele.data("tableSorting",_data);
                //触发回调函数并传参
                $ele.trigger("sorting-changed-callback",[name,type]);
            });
        }
    };

    old = $.fn.tableSorting;

    $.fn.tableSorting = function (option) {
        var args = arguments;
        var result = null;
        $(this).each(function (index, item) {
            var $this = $(item);
            var data = $this.data('tableSorting');
            var options = (typeof option !== 'object') ? null : option;
            if (!data) {
                data = new TableSorting(this, options);
                $this = $(data.$element);
                $this.data('tableSorting', data);
                return;
            }
            if (typeof option === 'string') {
                if (data[option]) {
                    result = data[option].apply(data, Array.prototype.slice.call(args, 1));
                } else {
                    throw "Method " + option + " does not exist";
                }
            } else {
                result = data.setOptions(option);
            }
        });
        return result;
    };


    $.fn.tableSorting.defaults = {
    	name:null,
    	type:null,
        onCallback : function(e,type){
            console.log(type);
        }
    };

    $.fn.tableSorting.Constructor = TableSorting;

}(window.jQuery));
